package com.sjgtw.cloud.common.core.web.domain;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>Title: 数据集封装类</p>
 * <p>Description: </p>
 * @author Admin
 * @date 2020-7-1
 */
public class ReportSealDate extends HashMap<String, Object>
{
	private static final long serialVersionUID = 1L;

	/** 返回对象字段名及字段类型 */
	public static final String FIELDS_TAG = "fields";

	/** 数据对象 */
	public static final String DATA_TAG = "data";

	public ReportSealDate() {
	}
	
	/**
	 * 
	 * <p>Title: 适用于对象数据返回</p>
	 * <p>Description: </p>
	 * @param data
	 * @param obj
	 */
	public ReportSealDate(Object data, Object obj) {
		super.put(DATA_TAG, data);
		super.put(FIELDS_TAG, getFiledName(obj));
	}
	
	/**
	 * <p>Title: 适用于MAP数据返回</p>
	 * <p>Description: </p>
	 * @param data
	 * @param obj
	 * @param bool
	 */
	public ReportSealDate(Object data, Object fields, boolean bool) {
		super.put(DATA_TAG, data);
		super.put(FIELDS_TAG, fields);
	}
	
	/**
	 * 
	 * <p>Title: 适用于对象数据返回</p>
	 * <p>Description: </p>
	 * @param data
	 * @param obj
	 */
	public static ReportSealDate success(Object data, Object obj) {
		return new ReportSealDate(data, obj);
	}
	
	/**
	 * 
	 * <p>Title: 适用于MAP数据返回</p>
	 * <p>Description: </p>
	 * @param data
	 * @param obj
	 * @param bool  = true
	 */
	public static ReportSealDate success(Object data, Object obj, boolean bool) {
		return new ReportSealDate(data, obj, bool);
	}
	
	
	/**
	 * <p>Title: 适用于对象数据返回</p>
	 * <p>Description: </p>
	 * @param data
	 * @param obj
	 * @return
	 */
	public static List<Map<String, Object>> getFiledName(Object obj) {
		Field[] fields = obj.getClass().getDeclaredFields();
		List<Map<String, Object>> filedls = new ArrayList<>();
		String fieldType = "text";
		for (int i = 0; i < fields.length; i++) {
			if(String.class == fields[i].getType()) {
				fieldType = "text";
			}
			else if ((Integer.TYPE == fields[i].getType()) || (Integer.class == fields[i].getType())) {
				fieldType = "numerical";
			} else if ((Long.TYPE == fields[i].getType()) || (Long.class == fields[i].getType())) {
				fieldType = "numerical";
			} else if ((Double.TYPE == fields[i].getType()) || (Double.class == fields[i].getType())) {
				fieldType = "numerical";
			} else if ((Float.TYPE == fields[i].getType()) || (Float.class == fields[i].getType())) {
				fieldType = "numerical";
			} else if (BigDecimal.class == fields[i].getType()) {
				fieldType = "numerical";
			} else if (Date.class == fields[i].getType()) {
				fieldType = "date";
			}
			Map<String, Object> fieldsMap = new HashMap<>();
			fieldsMap.put("fieldName", fields[i].getName());
			fieldsMap.put("fieldType", fieldType);
			filedls.add(fieldsMap);
		}
		return filedls;
	}

}
